package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Exam;
import cn.wolfcode.domain.Question;
import cn.wolfcode.enums.QuestionTypeEnum;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.JsonResult;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private IExamService examService;


    //跳转到试卷生成页面
    @RequestMapping("/input")
    public String input(){
        return "/exam/input";
    }
    //点击保存按钮之后，保存到数据库
    @RequiredPermission(name="试卷新增/编辑",expression="exam:save")
    @RequestMapping("/save")
    @ResponseBody
    public JsonResult list(Integer singleQuestionCount,
                           Integer multiQuestionCount,Integer judgementQuestionCount,Exam exam,
                          Integer singleScore,Integer multiScore,Integer judgementScore){
        //1.从question表中随机获得题目(传入的参数是试题类型和题数)
        List<Question> singleQuestion = examService.selectQuestion(QuestionTypeEnum.SINGLE.getValue(),singleQuestionCount);//单选题
        List<Question> multiQuestion = examService.selectQuestion(QuestionTypeEnum.MULITPLE.getValue(),multiQuestionCount);//多选题
        List<Question> jdugementQuestion = examService.selectQuestion(QuestionTypeEnum.JUDGEMENT.getValue(),judgementQuestionCount);//多选题
        //2.插入试卷名称和考试时长
        examService.insertTitleAndTime(exam);
       // 3.插入随机拿到的题目到exam_question(试卷题目)表中:单选，多选，以及判断分别插入
        examService.insertRandomQuestion(singleQuestion,exam.getId(),singleScore);
        examService.insertRandomQuestion(multiQuestion,exam.getId(),multiScore);
        examService.insertRandomQuestion(jdugementQuestion,exam.getId(),judgementScore);

        return new JsonResult(exam.getId());
    }
    @RequestMapping("/show")
    public String show(Model model,Long id){
        //根据id查询,这是exam的id
        Exam exam = examService.selectExamPaperByExamId(id);
        model.addAttribute("exam",exam);
        return "/exam/show";
    }


}
